package com.mccken.rapid.core.helper;

import com.mccken.rapid.common.enums.ResponseCode;
import com.mccken.rapid.core.context.RapidResponse;

import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

/**
 * @program: rapid_gateway
 * @description:
 * @author: mccken
 * @create: 2022-01-16 20:04
 **/
public class ResponseHelper {
	
	public static FullHttpResponse getHttpResponse(ResponseCode responseCode) {
		RapidResponse rapidResponse = RapidResponse.buildRapidResponse(responseCode);
		DefaultFullHttpResponse httpResponse = 
				new DefaultFullHttpResponse(
						HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, 
						Unpooled.wrappedBuffer(rapidResponse.getContent().getBytes()));
		
		httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON + ";charset=utf-8");
		httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, httpResponse.content().readableBytes());
		return httpResponse;
	}
}
